package com.uduemc.biso.module.basic.service.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.page.PageMethod;
import com.uduemc.biso.core.extities.center.SysServer;
import com.uduemc.biso.module.basic.service.mapper.SysServerMapper;
import com.uduemc.biso.module.basic.service.service.SysServerService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class SysServerServiceImpl implements SysServerService {

	@Autowired
	private SysServerMapper serverMapper;

	@Override
	public List<SysServer> fintAll() {
		return serverMapper.selectAll();
	}

	@Override
	public List<SysServer> fintOkAll() {
		Example example = new Example(SysServer.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("status", 1);
		List<SysServer> list = serverMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list;
	}

	@Override
	public SysServer findOne(Long id) {
		return serverMapper.selectByPrimaryKey(id);
	}

	@Override
	public SysServer insert(SysServer server) {
		serverMapper.insert(server);
		return findOne(server.getId());
	}

	@Override
	public boolean updateByPrimaryKey(SysServer server) {
		return serverMapper.updateByPrimaryKey(server) > 0;
	}

	@Override
	public boolean deleteByPrimaryKey(Long id) {
		return serverMapper.deleteByPrimaryKey(id) > 0;
	}

	@Override
	public SysServer findOkOne(Long id) {
		Example example = new Example(SysServer.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("id", id);
		criteria.andEqualTo("status", 1);
		List<SysServer> list = serverMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public SysServer updateById(SysServer server) {
		serverMapper.updateByPrimaryKey(server);
		return findOne(server.getId());
	}

	@Override
	public List<SysServer> findAll(Pageable pageable) {
		int pageNumber = pageable.getPageNumber();
		int pageSize = pageable.getPageSize();
		PageMethod.startPage(pageNumber, pageSize);
		return serverMapper.selectAll();
	}

	@Override
	public int deleteById(Long id) {
		return serverMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<SysServer> findByTypeStatus(int type, short status) {
		Example example = new Example(SysServer.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("type", type);
		if (status != (short) -1) {
			criteria.andEqualTo("status", status);
		}
		List<SysServer> list = serverMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list;
	}

}
